package edu.upenn.pcr.async;

import java.util.List;

import android.content.Context;
import edu.upenn.pcr.controller.Controller;
import edu.upenn.pcr.controller.DefaultTestingController;
import edu.upenn.pcr.model.async.FetchSectionByCourseAsync;
import edu.upenn.pcr.model.db.PennCourseReviewDBMgr;
import edu.upenn.pcr.model.db.dao.CourseDAO;
import edu.upenn.pcr.model.db.dao.CourseDetailDAO;
import edu.upenn.pcr.model.db.dao.CourseSectionDAO;
import edu.upenn.pcr.model.db.dao.InstructorDAO;
import edu.upenn.pcr.model.db.dao.SemesterDAO;
import edu.upenn.pcr.model.db.entity.Course;
import edu.upenn.pcr.model.db.entity.CourseSection;
import edu.upenn.pcr.model.db.entity.Instructor;
import edu.upenn.pcr.model.db.entity.Semester;
import edu.upenn.pcr.test.AndroidTestCaseBase;

public class FetchCourseSectionAsyncTest extends AndroidTestCaseBase {

	private Controller controller;
	PennCourseReviewDBMgr dbMgr;
	
	protected void setUp() throws Exception {
		super.setUp();
		Context context = this.getTestContext();
		controller = new DefaultTestingController(context);
		dbMgr = PennCourseReviewDBMgr.getInstance(controller.getContext());
	}
	
	public void testFetchCourseSection() {
		FetchSectionByCourseAsync task = new FetchSectionByCourseAsync(0, controller);
		Instructor instr = new Instructor();
		instr.setFirstName("SUDIPTO");
		instr.setLastName("GUHA");
		instr.setId("298-SUDIPTO-GUHA");
		instr.setViewed(0);
	
		Course course = new Course();
		course.setId("309");
		course.setName("PROG LANG AND TECH II");
		course.setViewed(0);
			
		Semester semester = new Semester();
		semester.setId("2002A");
		semester.setYear(2002);
		semester.setSeason("Spring");
		semester.setName("Spring 2002");

		CourseDAO courseDAO = dbMgr.getCourseDAO();
		InstructorDAO instrDAO = dbMgr.getInstructorDAO();
		SemesterDAO semesterDAO = dbMgr.getSemesterDAO();
		
		instrDAO.add(instr);
		courseDAO.add(course);
		semesterDAO.add(semester);
		
		task.test("309");
		CourseSectionDAO csDAO = dbMgr.getCourseSectionDAO();
		CourseDetailDAO cdDAO = dbMgr.getCourseDetailDAO();
		
		int cdCount = cdDAO.count();
		assertTrue(cdCount > 0);
		
		int count = csDAO.count();
		assertTrue(count>0);
		
		List<CourseSection> csList = csDAO.findByCourseId("309");
		assertTrue(csList.size()>0);
	}
}
